package com.youlu.campus.service.activity.impl;

import com.mongodb.client.result.DeleteResult;
import com.youlu.campus.entity.ActivityVideoLike;
import com.youlu.campus.service.activity.ActivityVideoLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ActivityVideoLikeServiceImpl implements ActivityVideoLikeService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public ActivityVideoLike findBy(String userId, String activityId, String mid) {
        Query q = new Query();
        q.addCriteria(Criteria.where("userId").is(userId).and("activityId").is(activityId).and("mid").is(mid));
        return mongoTemplate.findOne(q, ActivityVideoLike.class);
    }

    @Override
    public boolean insert(ActivityVideoLike a) {
        a.setCreatedTime(new Date());
        a.setId(null);
        a = mongoTemplate.insert(a);
        return a.getId() == null ? false : true;
    }

    @Override
    public boolean delete(String id) {
        Query q = new Query();
        q.addCriteria(Criteria.where("id").is(id));
        DeleteResult d = mongoTemplate.remove(q, ActivityVideoLike.class);
        return d.getDeletedCount() > 0 ? true : false;
    }

    @Override
    public Long getCount(String activityId, String mid) {
        Query q = new Query();
        q.addCriteria(Criteria.where("activityId").is(activityId).and("mid").is(mid));
        return mongoTemplate.count(q, ActivityVideoLike.class);
    }
}
